<?php

namespace App\Http\Controllers\Carousel;

use App\Models\Carousel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CarouselController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $carousels=Carousel::get();
    return view('carousel.carousel.index',compact('carousels'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       return view('carousel.carousel.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'title'=>'required',
            'icon'=>'required',
            'sign'=>'required',
        ],[
        'title.required'=>'主题不能为空',
        'icon.required'=>'请上传图片',
        'sign.required'=>'请填写标识,区分给那块用',
        ]);
        if(isset($request->status)&&$request->status==1){
            //表示这个主题的轮播图将要被使用,要把其他使用的轮播图的状态设为0,然后添加数据
            $carousel=Carousel::where('sign',$request->sign)->where('status',1)->first();
           $carousel['status']=0;
           $carousel->save();
           Carousel::create($request->all());
            return redirect()->route('carousel.carousel.index')->with('success','添加成功');
            
        }else{
            Carousel::create($request->all());
            return redirect()->route('carousel.carousel.index')->with('success','添加成功');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Carousel  $carousel
     * @return \Illuminate\Http\Response
     */
    public function show(Carousel $carousel)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Carousel  $carousel
     * @return \Illuminate\Http\Response
     */
    public function edit(Carousel $carousel)
    {
       return view('carousel.carousel.edit',compact('carousel'));
    }
    public function update(Request $request, Carousel $carousel)
    {
        $request->validate([
            'title'=>'required',
            'icon'=>'required',
            'sign'=>'required',
        ],[
            'title.required'=>'主题不能为空',
            'icon.required'=>'请上传图片',
            'sign.required'=>'请填写标识,区分给那块用',
        ]);
        if(isset($request->status)&&$request->status==1){
            //表示这个主题的轮播图将要被使用,要把其他使用的轮播图的状态设为0,然后添加数据
            $carousels=Carousel::where('sign',$request->sign)->get();
            $carousels['status']=0;
            $carousels->save();
            $carousel->update($request->all());
            return redirect()->route('carousel.carousel.index')->with('success','修改成功');
        
        }else{
            if (isset($carousel['status'])&&$carousel['status']==1){
                $carousels=Carousel::where('sign',$request->sign)->first();
                $carousels['status']==1;
                return back()->with('success','你未选择主题,我们将默认为你设置一套');
            }else{
                Carousel::create($request->all());
                return redirect()->route('carousel.carousel.index')->with('success','修改成功');
            }
           
        }
        
    }

    public function destroy(Carousel $carousel)
    {
//        dd($carousel);
        $carousels=Carousel::where('sign',$carousel->sign)->get();
//        dd(count($carousels));
  if (count($carousels)<2||$carousel['status']==1){
     
      return back()->with('error','为保证首页展示正常请先添加'.$carousel['sign'].'下的轮播图并设置成正在使用再来删除');
  }
  else{
      $carousel->delete();
      return redirect()->route('carousel.carousel.index')->with('success','删除成功');
  }
    }
}
